const { mongoose } = require("../utils/mongoose")

//定义schema 约定存入哪些字段
const schema = mongoose.Schema({
    token_id: { type: Number, required: true },
    companyName: { type: String, required: true },
    positionName: { type: String, required: true },
    city: { type: String, required: true },
    salary: { type: String, required: true },
    degree: { type: String, required: true },
    type: { type: String, required: true },
    experience: { type: String, required: true },
    description: { type: String, required: true },
    createTime: { type: String, require: true },
    companyLogo: { type: String }
})

// 调用保存的方法
//得到一个叫做positions的集合
const positions = mongoose.model("positions", schema)
//增加
const save = (query) => {
    const pos = new positions(query)
    return pos.save()
        .then(res => res)
        .catch(err => {
            console.log(err)
        })
}

//find
const find = (info) => {
    let { page, count } = info
    return positions.find(info.query).limit(count).skip(page)
        .then(res => res)
        .catch(err => {
            console.log(err)
        })
}

//删除
const del = (token_id) => {
    return positions.deleteOne({ token_id: token_id })
        .then(res => res)
        .catch(err => {
            console.log(err)
        })
}

//更新token_id

const updateToken = (token_id) => {
    console.log(typeof token_id)
    let query = { token_id: { $gt: token_id } };
    let updated = { $inc: { token_id: -1 } }
    return positions.updateMany(query, updated)//1.query查询条件 2.updated需要更新的内容
        .then(res => res)
        .catch(err => {
            console.log(err)
        })
}
//更新职位信息
const update = (query, updated) => {
    return positions.updateOne(query, updated)
        .then(res => { console.log(res) })
        .catch(err => {
            console.log(err)
        })
}

module.exports = {
    save,
    find,
    del,
    updateToken,
    update
}
// 查找的方法